﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Universum.Infrastructure.Configuration;
using System.Security.Principal;
using System.Web.Mvc;

namespace Universum.Infrastructure.Utils.Extensions
{
	/// <summary>
	/// Specific extensions for the current user
	/// </summary>
	public static class  LoggedInUserExtensions
	{
		public static bool LoggedInUserIsTeacherOrStudent(this HtmlHelper html)
		{
			return html.LoggedInUser().IsInRole(Universum.Infrastructure.Configuration.ConstantIdentifiers.ROLE_OF_TEACHER) ||
				html.LoggedInUser().IsInRole(Universum.Infrastructure.Configuration.ConstantIdentifiers.ROLE_OF_STUDENT);
		}
		public static bool LoggedInUserIsStudent(this HtmlHelper html)
		{
			return html.LoggedInUser().IsInRole(ConstantIdentifiers.ROLE_OF_STUDENT);
		}
		public static bool LoggedInUserIsTeacher(this HtmlHelper html)
		{
			return html.LoggedInUser().IsInRole(ConstantIdentifiers.ROLE_OF_TEACHER);
		}

		public static bool IsSpecialUserLoggedIn(this HtmlHelper html)
		{
			return html.LoggedInUser().Identity.Name == ConstantIdentifiers.SPECIAL_USER;
		}
		public static IPrincipal LoggedInUser(this HtmlHelper htmlHelper)
		{
			return HttpContext.Current.User;
		}
		public static string UsernameInRequest(this HtmlHelper htmlHelper)
		{
			if (htmlHelper.ViewContext.RouteData.Values.ContainsKey("username"))
				return htmlHelper.ViewContext.RouteData.Values["username"].ToString();
			return null;
		}
		public static bool IsOwnerLoggedIn(this HtmlHelper htmlHelper, string owner)
		{
			return owner == htmlHelper.LoggedInUser().Identity.Name;
		}
	}
}
